/*
 * @Author       : LuHeQiu
 * @Date         : 2021-04-01 18:51:38
 * @LastEditTime : 2021-04-01 18:51:39
 * @LastEditors  : LuHeQiu
 * @Description  : 
 * @FilePath     : \User\task.h
 * @HomePage     : https://www.luheqiu.com
 */

#ifndef __TASK_H
#define __TASK_H

#include "stm32f0xx.h"

/* Set default parameters ----------------------------------------------------*/

#define MAX_TASK_NUM 6              //Maximum number of tasks
#define MAX_TICK_NUM 10             //Maximum number of ticks
#define TASKSYSTEM_PERIOD 10        //Period of TaskSystem (ms)

/* Declaration of system variables -------------------------------------------*/

extern uint8_t b100usFlag;

typedef enum{
    sleep,     //休眠态
    ready,     //就绪态
    running,   //运行态
    waiting,   //等待态
    release    //释放态 
}TaskState;

typedef struct{
    uint8_t (*taskFunction)(uint8_t);  //任务函数指针
    uint8_t taskTicks;                 //任务占用的时间片
    uint16_t defaultPeriod;            //任务默认周期
    uint8_t taskPriority;              //任务优先级
    TaskState taskstate;               //任务状态

    uint16_t cTaskPerid;             //当前任务周期      
}Task;

typedef struct{
    Task tasks[MAX_TASK_NUM+1];     //任务池
    uint8_t maxTaskNum;             //最大任务数量
    uint8_t resTaskNum;             //剩余任务数量
    uint8_t maxTickNum;             //最大时间片数量
    uint8_t resTickNum;             //剩余时间片数量
}TaskStack;


/* Declaration of system functions ------------------------------------------*/

void InitTaskSystem(void);
void LaunchTaskSchedule(void);
void CloseTaskSchedule(void);
void TaskScheduleLoop(void);
uint8_t CreateTask( uint8_t (*taskFunction)(uint8_t),uint8_t ticks,uint16_t period);
uint8_t RunningTask( uint8_t taskID,uint8_t* taskTicks);
uint8_t StopTask(uint8_t (*taskFunction)(uint8_t));
uint8_t SleepTask(uint8_t (*taskFunction)(uint8_t));

#endif
